package com.lml.dao;

import java.io.Serializable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lml.common.Constants;
import com.lml.po.BasePO;



public class HibernaeDaoImpl extends HibernateDaoSupport implements
		ISingleTableDao {
	protected static final Log log = LogFactory.getLog(HibernaeDaoImpl.class);

	public void insert(BasePO basePO) throws DataAccessException {
		getHibernateTemplate().save(basePO);
	}

	public void insert(List<BasePO> listBasePO) throws DataAccessException {
		for (int i = 0; i < listBasePO.size(); ++i) {
			BasePO BasePO = (BasePO) listBasePO.get(i);
			getHibernateTemplate().save(BasePO);
		}
	}

	public void update(BasePO BasePO) throws DataAccessException {
		getHibernateTemplate().update(BasePO);
	}

	public void update(List<BasePO> listBasePO) throws DataAccessException {
		for (int i = 0; i < listBasePO.size(); ++i) {
			BasePO BasePO = (BasePO) listBasePO.get(i);
			getHibernateTemplate().update(BasePO);
		}
	}

	public void update(String strUpdSql) throws DataAccessException {
		Query query = getSession().createQuery(strUpdSql);
		query.executeUpdate();
	}

	public void delete(BasePO BasePO) throws DataAccessException {
		getHibernateTemplate().delete(BasePO);
	}

	public void delete(List<BasePO> listBasePO) throws DataAccessException {
		for (int i = 0; i < listBasePO.size(); ++i) {
			BasePO BasePO = (BasePO) listBasePO.get(i);
			delete(BasePO);
		}
	}

	public List selectSplit(String sql, Integer firstResult, Integer maxResults) throws DataAccessException {
		Query query = getSession().createQuery(sql);
		query.setFirstResult((firstResult - 1) * Constants.PAGESIZE_DEF);
		query.setMaxResults(maxResults);
		return query.list();
	}

	public void saveOrUpdate(BasePO BasePO) {
		getHibernateTemplate().saveOrUpdate(BasePO);
	}

	public void saveOrUpdate(List<BasePO> listBasePO) {
		for (int i = 0; i < listBasePO.size(); ++i) {
			BasePO BasePO = (BasePO) listBasePO.get(i);
			getHibernateTemplate().saveOrUpdate(BasePO);
		}
	}

	public BasePO get(BasePO BasePO, Serializable key)
			throws DataAccessException {
		return (BasePO) getHibernateTemplate().get(BasePO.getClass(),
				"BasePO.getStrKeyWhere()");
	}

	public void exeSql(StringBuffer sbSql) throws DataAccessException {
		Query query = getSession().createQuery(sbSql.toString());
		query.executeUpdate();
	}

	public BasePO selectBasePO(BasePO basePO){
//		List listBasePO = getHibernateTemplate().find("from" + basePO.getStrTableName() + "where" + basePO.getStrPKValues());
//		if(listBasePO == null || listBasePO.size() == 0){
//			return null;
//		}
//		return (BasePO) listBasePO.get(0);
		return getHibernateTemplate().get(basePO.getClass(), basePO.getId());
	}
	
	public String queryString(String sql){
		Query query = getSession().createQuery(sql);
		return query.getQueryString();
	}
	
	public HibernaeDaoImpl getFromApplicationContext(ApplicationContext ctx) {
		return (HibernaeDaoImpl) ctx.getBean("HibernaeDaoImpl");
	}

}